<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px" size="small">
      <el-form-item label="报修单号" prop="repairCode">
        <el-input
          v-model="queryParams.repairCode"
          placeholder="请输入报修单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="equipmentName">
        <el-input
          v-model="queryParams.equipmentName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报修状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择报修状态" clearable>
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="报修时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="repairList">
      <el-table-column label="维修单号" align="center" prop="repairCode" />
      <el-table-column label="设备名称" align="center" prop="equipmentName" />
      <el-table-column label="故障描述" align="center" prop="faultDescription" :show-overflow-tooltip="true" />
      <el-table-column label="报修人" align="center" prop="reportPerson" />
      <el-table-column label="报修时间" align="center" prop="reportTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reportTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="维修状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === 'pending' ? 'warning' : scope.row.status === 'processing' ? 'primary' : scope.row.status === 'completed' ? 'success' : 'info'">
            {{ scope.row.status === 'pending' ? '待处理' : scope.row.status === 'processing' ? '处理中' : scope.row.status === 'completed' ? '已完成' : '已关闭' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="210">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
            v-hasPermi="['sb:repair:query']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="scope.row.status === 'pending'"
            @click="handleProcess(scope.row)"
            v-hasPermi="['sb:repair:edit']"
          >处理</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            v-if="scope.row.status === 'processing'"
            @click="handleComplete(scope.row)"
            v-hasPermi="['sb:repair:edit']"
          >完成维修</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sb:repair:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    
    <!-- 维修完成对话框 -->
    <el-dialog title="完成维修" :visible.sync="completeDialogVisible" width="500px" append-to-body>
      <el-form ref="completeForm" :model="completeForm" :rules="completeRules" label-width="100px">
        <el-form-item label="维修结果" prop="handleResult">
          <el-input v-model="completeForm.handleResult" type="textarea" placeholder="请输入维修结果描述" :rows="3"></el-input>
        </el-form-item>
        <el-form-item label="维修费用" prop="cost">
          <el-input-number v-model="completeForm.cost" :precision="2" :min="0" :step="10"></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitComplete">确 定</el-button>
        <el-button @click="cancelComplete">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRepairOrder, getRepairOrder, delRepairOrder, processRepair, completeRepair, cancelRepair } from "@/api/sb/repairOrder";

export default {
  name: "RepairOrder",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报修单表格数据
      repairList: [],
      // 报修状态选项
      statusOptions: [
        { dictLabel: '待处理', dictValue: 'pending' },
        { dictLabel: '处理中', dictValue: 'processing' },
        { dictLabel: '已完成', dictValue: 'completed' },
        { dictLabel: '已关闭', dictValue: 'closed' }
      ],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        repairCode: undefined,
        equipmentName: undefined,
        status: undefined
      },
      // 维修完成对话框
      completeDialogVisible: false,
      // 维修结果表单
      completeForm: {
        repairId: undefined,
        handleResult: '',
        cost: 0
      },
      // 维修结果表单规则
      completeRules: {
        handleResult: [
          { required: true, message: '请输入处理结果', trigger: 'blur' }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询报修单列表 */
    getList() {
      this.loading = true;
      listRepairOrder(this.queryParams).then(response => {
        this.repairList = response.rows;
        this.total = parseInt(response.total) || 0;
        this.loading = false;
      });
    },
    // 报修状态字典翻译
    statusFormat(row) {
      for (let i = 0; i < this.statusOptions.length; i++) {
        if (this.statusOptions[i].dictValue === row.status) {
          return this.statusOptions[i].dictLabel;
        }
      }
      return '';
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.$router.push({ path: '/sb/repair-order/detail', query: { id: row.repairId } });
    },
    /** 处理按钮操作 */
    handleProcess(row) {
      this.$modal.confirm('确认开始处理该维修单?').then(() => {
        const data = {
          repairId: row.repairId,
          status: 'processing',
          handlePerson: this.$store.getters.name,
          handleTime: new Date()
        };
        
        processRepair(data).then(response => {
          this.$modal.msgSuccess('开始处理成功');
          this.getList();
        }).catch(error => {
          this.$modal.msgError('处理失败: ' + error);
        });
      }).catch(() => {});
    },
    
    /** 完成维修按钮操作 */
    handleComplete(row) {
      this.completeForm.repairId = row.repairId;
      this.completeForm.handleResult = '';
      this.completeForm.cost = 0;
      this.completeDialogVisible = true;
    },
    
    /** 提交维修结果 */
    submitComplete() {
      this.$refs.completeForm.validate(valid => {
        if (valid) {
          const data = {
            repairId: this.completeForm.repairId,
            handleResult: this.completeForm.handleResult,
            cost: this.completeForm.cost,
            status: 'completed',
            closeTime: new Date()
          };
          
          completeRepair(data).then(response => {
            this.completeDialogVisible = false;
            this.$modal.msgSuccess('维修完成');
            this.getList();
          }).catch(error => {
            this.$modal.msgError('提交失败: ' + error);
          });
        }
      });
    },
    
    /** 取消完成对话框 */
    cancelComplete() {
      this.completeDialogVisible = false;
    },
    
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.repairId;
      this.$modal.confirm('是否确认删除该维修单？').then(function() {
        return delRepairOrder(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script> 